package com.xx.jenkinsdemo.listener;

import com.xx.jenkinsdemo.vo.MessageBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @title: RabbitMqListener
 * @Author xu-xiang
 * @Date: 2025/7/8 11:10
 * @description:
 */
@Component
@Slf4j
public class RabbitMqListener {

    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue(
                                    name = "deadLetterQueue"  //死信队列
                            ),
                            exchange = @Exchange(value = "baseDeadLetterExchange"),
                            key = {"baseDeadLetterKey"}
                    )
            }
    )
    public void handleDeadLetter(Message message, @Payload MessageBean messageBean) {
        log.info("开始消费MQ消息__时间: {}", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        log.info("已经消费死信队列mq任务: {}", message);
        log.info("已经消费死信队列mq任务: {}", messageBean.getBody());
    }

    @RabbitListener(queues = {"delayedQueue"})
    public void handleDelayedLetter(Message message, org.springframework.messaging.Message<Object> genericMessage, @Payload MessageBean messageBean) {
        log.info("开始消费MQ延迟消息__时间: {}", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        log.info("已经消费延迟队列mq任务: {}", message);
        log.info("已经消费延迟队列mq任务: {}", messageBean.getBody());
    }
}
